package com.hxkj.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxkj.dao.NoticeDao;
import com.hxkj.domain.enums.NoticeExceptionEnum;
import com.hxkj.domain.po.Notice;
import com.hxkj.service.NoticeService;
import com.hxkj.utils.StrSliptUtils;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import com.platform.common.domain.to.PageTo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 公告Service实现类
 * <p>
 * Created by chenfangzhou on 2018/6/6.
 */
@Service
@Slf4j
public class NoticeServiceImpl implements NoticeService {

    @Autowired
    private NoticeDao noticeDao;

    /**
     * 保存单条公告
     *
     * @param notice
     * @return
     * @throws Exception
     */
    @Override
    public ResultMessageStruct saveNotice(Notice notice) throws Exception {
        ResultMessageStruct resultMessageStruct = new ResultMessageStruct();
        // 判断公告是否已经存在，如果存在则不允许重复添加
        Notice newnotice = new Notice();
        newnotice.setNoticeTitle(notice.getNoticeTitle());
        newnotice.setDelTag(0);
        newnotice.setSiteId(notice.getSiteId());
        try {
            if (noticeDao.getNoticeList(newnotice).size() > 0) {
                resultMessageStruct.setCode(NoticeExceptionEnum.NOTICE_EXIST_ALREADY.getCode());
                return resultMessageStruct;
            } else {
                notice.setCreateTime(System.currentTimeMillis());
                int i = noticeDao.save(notice);
                resultMessageStruct.setCode(200);
                return resultMessageStruct;
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultMessageStruct.setCode(NoticeExceptionEnum.NOTICE_SAVE_ERROR.getCode());
        }
        return resultMessageStruct;
    }


    /**
     * 获取公告列表,带分页数据
     *
     * @param notice
     * @param pageParam
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo<Notice> getNoticeList(Notice notice, PageTo pageParam) throws Exception {
        try {
            if (pageParam.getPageNum() == null || pageParam.getPageSize() == null) {
                pageParam.setPageNum(1);
                pageParam.setPageSize(10);
            }
            Page<Notice> page = PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            notice.setDelTag(0);
            notice.setIsEnable(0);
            noticeDao.getNoticeList(notice);
            PageInfo<Notice> pageInfo = new PageInfo<>(page);
            return pageInfo;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    /**
     * 获取单条公告
     *
     * @param notice
     * @return
     * @throws Exception
     */
    @Override
    public ResultDataStruct<Notice> getNotice(Notice notice) throws Exception {
        ResultDataStruct<Notice> resultDataStruct = new ResultDataStruct<>();
        try {
            Notice newnotice = noticeDao.get(notice.getNoticeId());

            if (newnotice == null) {
                resultDataStruct.setCode(NoticeExceptionEnum.NOTICE_IS_NULL.getCode());
                return resultDataStruct;
            } else {
                //已经删除
                if (newnotice.getDelTag() == 1) {
                    resultDataStruct.setCode(NoticeExceptionEnum.NOTICE_IS_NULL.getCode());
                    return resultDataStruct;
                } else {
                    resultDataStruct.setCode(200);
                    resultDataStruct.setData(newnotice);
                    return resultDataStruct;
                }
            }
        } catch (Exception e) {
            resultDataStruct.setCode(NoticeExceptionEnum.NOTICE_QUERY_ERROR.getCode());
            e.printStackTrace();
            return resultDataStruct;
        }
    }


    /**
     * 删除公告,可批量删除,也可单条删除
     *
     * @param notice
     * @return
     */
    @Override
    public ResultMessageStruct deleteNotice(Notice notice) {
        ResultMessageStruct resultMessageStruct = new ResultDataStruct<>();
        try {

            // 删除多条
            if (notice.getNoticeIds() != null && notice.getNoticeIds() != "") {
                String[] str = StrSliptUtils.sliptArray(notice.getNoticeIds());
                for (int i = 0; i < str.length; i++) {

                    noticeDao.delete(Long.valueOf(str[i]));

                }
            } else {// 删除单条
                noticeDao.delete(notice.getNoticeId());
            }
            resultMessageStruct.setCode(200);
            return resultMessageStruct;
        } catch (NumberFormatException e) {
            e.printStackTrace();
            resultMessageStruct.setCode(NoticeExceptionEnum.NOTICE_DELETE_ERROR.getCode());
            return resultMessageStruct;
        }
    }

    /**
     * 更新公告
     *
     * @param notice
     * @return
     * @throws Exception
     */
    @Override
    public ResultMessageStruct updateNotice(Notice notice) throws Exception {
        ResultMessageStruct result = new ResultMessageStruct();
        try {
            if (StringUtils.isNoneBlank(notice.getNoticeTitle())) {
                // 判断链接地址是否已经存在，如果存在则不允许重复添加
                Notice noticeTitle = noticeDao.selectNoticeTitle(notice);
                // 判断传入的公告标题是否已经存在，如果存在则不允许重复添加
                if (noticeTitle != null) {
                    result.setCode(NoticeExceptionEnum.NOTICE_EXIST_ALREADY.getCode());
                    return result;
                }
            }
            notice.setUpdateTime(System.currentTimeMillis());
            int i = noticeDao.update(notice);
            result.setCode(i > 0 ? 200 : NoticeExceptionEnum.NOTICE_UPDATE_ERROR.getCode());
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(NoticeExceptionEnum.NOTICE_UPDATE_ERROR.getCode());
            return result;
        }
    }

}
